package cst.cn.edu.neusoft.xumingze224.yuejian_5.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import cst.cn.edu.neusoft.xumingze224.yuejian_5.Bean.RegisterBean;
import cst.cn.edu.neusoft.xumingze224.yuejian_5.Listener.TListener;
import cst.cn.edu.neusoft.xumingze224.yuejian_5.Model.RegisterModel;
import cst.cn.edu.neusoft.xumingze224.yuejian_5.R;

public class RegisterActivity extends BaseActivity {
    EditText et_username,et_userpass,et_repassword,et_phone;
    Button btn_registe,btn_return;
    TListener<RegisterBean> registerListener=new TListener<RegisterBean>() {

        @Override
        public void onResponse(RegisterBean registerBean) {
            if(registerBean.getSuccess()==0){
                showToast("该用户已注册");
                et_username.setText("");
                et_userpass.setText("");
                et_repassword.setText("");
                et_phone.setText("");

            }
            else {
                showToast("注册成功");
                saveInfo();
                returnMsg();
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };
    @Override
    void initView() {
        setLayout_filel(R.layout.activity_register);
        et_username=(EditText)findViewById(R.id.et_username);
        et_userpass=(EditText)findViewById(R.id.et_password);
        et_repassword=(EditText)findViewById(R.id.et_repassword);
        et_phone=(EditText)findViewById(R.id.et_tel);

        btn_registe=(Button)findViewById(R.id.btn_registe);
        btn_return=(Button)findViewById(R.id.btn_return);

    }

    @Override
    void initEvent() {
        btn_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intend=new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intend);
            }
        });
        btn_registe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = et_username.getText().toString();
                String mobilenum = et_phone.getText().toString();
                String p1 = et_userpass.getText().toString();
                String p2 = et_repassword.getText().toString();
                if(username.trim().equals("")||p1.trim().equals("")||p2.trim().equals("")||mobilenum.trim().equals("")
                        ){
                    showToast("不能为空");
                }
                else {
                    if (p1.equals(p2)) {
                        RegisterModel registerModel = new RegisterModel();
                        registerModel.getRegister(username, p1, mobilenum, registerListener);
                    }
                    else showToast("密码不一致");
                }
            };
//setonclicklistener
        });
    }
    @Override
    void initData() {
    }
    public void returnMsg(){
        String message=et_username.getText().toString();
        Intent intent1=new Intent();
        intent1.putExtra("recieve",message);
        setResult(RESULT_OK,intent1);
        finish();
    }
    public void saveInfo(){
        SharedPreferences sp=getSharedPreferences("userInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("phone",et_phone.getText().toString());
        editor.commit();
    }



}